package net.crisps.cloud.utils.robot_util.service.default_config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.crisps.cloud.utils.common_util.CustomStrUtil;
import net.crisps.cloud.utils.hander.ExceptionHandler;
import net.crisps.cloud.utils.robot_util.vo.WeChatRobotConfigVo;
import net.crisps.cloud.utils.vo.ExceptionLogVo;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

/**
 * @author SilenceShine
 * @github <a href="https://github.com/SilenceShine">SilenceShine</a>
 * @since 1.0
 */
@Slf4j
@Component
public class DefaultWeChatRobotExceptionHandler implements ExceptionHandler {


    @Autowired
    AbstractWechatRobotConfigService abstractWechatRobotConfigService;

    /**
     * 默认所有人
     */
    private static final String MENTIONED_MOBILE_LIST = "@all";
    /**
     * 消息长度
     */
    private static final int MSG_MAX_LENGTH = 5120;

    @Override
    public void process(ExceptionLogVo exceptionLogVo) {
        WeChatRobotConfigVo config = abstractWechatRobotConfigService.getWeChatRobotConfigVo();
        if(config==null|| CustomStrUtil.isBlank(config.getWechatRobotUrl())){
            log.error("=====WeChatRobotExceptionHandler===机器字典配置为空====");
            return;
        }
        String msg = initErrMsg(exceptionLogVo,config );
        log.error("========sendMsg===={}",msg);
        msg = msg.length() > MSG_MAX_LENGTH ? msg.substring(0, MSG_MAX_LENGTH) : msg;
        JSONObject objectText = new JSONObject();
        objectText.put("content", msg);
        objectText.put("mentioned_mobile_list", CollectionUtils.isEmpty(config.getNotifyUserPhone()) ? MENTIONED_MOBILE_LIST : config.getNotifyUserPhone());
        JSONObject object = new JSONObject();
        object.put("msgtype", "text");
        object.put("text", objectText);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", StandardCharsets.UTF_8));
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(config.getWechatRobotUrl(), new HttpEntity<>(object.toString(), headers), String.class);
        if (responseEntity.getStatusCodeValue() != HttpStatus.SC_OK) {
            log.error("======WeChatRobotUtil机器人发送消息失败：{}========", JSON.toJSONString(responseEntity));
        }
    }

    /**
     * 初始化异常错误信息
     */
    private String initErrMsg(ExceptionLogVo exceptionVo,WeChatRobotConfigVo config) {
        return "【登录人信息】" + config.getLoginUserInfo() +
                "\n" +
                "【项目】" + config.getProjectAppId() + "/" + config.getProjectName() +
                "\n" +
                "【system】" + exceptionVo.getSysCode() +
                " 【ip】" + exceptionVo.getIp() +
                "\n" +
                "【port】" + exceptionVo.getServerPort() +
                "【远程端口】" + exceptionVo.getRemotePort() +
                "\n" +
                "【class】" + exceptionVo.getClassName() +
                "【method】(" + exceptionVo.getMethod() + ")" + exceptionVo.getRequestUri() +
                "\n" +
                "【params】" + exceptionVo.getParams() +
                "\n" +
                "【exception】" + exceptionVo.getExceptionStr();
    }


}
